package com.qiaofang.applet.rest;

import com.google.common.collect.Lists;
import com.qiaofang.applet.common.context.QfContextHolder;
import com.qiaofang.applet.common.dto.DataRequest;
import com.qiaofang.applet.common.dto.DataResponse;
import com.qiaofang.applet.common.dto.PageDataRequest;
import com.qiaofang.applet.common.util.PageUtil;
import com.qiaofang.applet.service.member.domain.MemberHouseInfo;
import com.qiaofang.applet.service.member.service.MemberBrowseService;
import com.qiaofang.applet.service.member.service.MemberHouseInfoService;
import com.qiaofang.applet.service.member.service.MemberService;
import com.qiaofang.applet.service.property.domain.Property;
import com.qiaofang.applet.service.property.domain.PropertyContact;
import com.qiaofang.applet.service.property.service.PropertyContactService;
import com.qiaofang.applet.service.property.service.PropertyService;
import com.qiaofang.applet.service.property.service.dto.*;
import com.qiaofang.applet.service.property.service.mapstruct.PropertyMapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @author daiyuxaing
 */
@Slf4j
@RestController
@RequestMapping("/api/web/auth/propertyContact")
@Api(tags = "会员：我的房子")
public class PropertyContactController {

    @Autowired
    MemberService membeService;

    @Autowired
    MemberBrowseService memberBrowseService;

    @Autowired
    private PropertyService propertyService;

    @Autowired
    private PropertyContactService propertyContactService;

    @Autowired
    private PropertyMapper propertyMapper;

    @Autowired
    private MemberHouseInfoService memberHouseInfoService;


    @PostMapping("/searchMySaleProperty")
    @ApiOperation(value = "查询我的二手房列表", notes = "查询我的二手房列表")
    public DataResponse<List<SalePropertyListDto>> searchMySaleProperty(@RequestBody @Valid DataRequest<PropertyContactQueryDto> request) {
        List<SalePropertyListDto> result = null;

        List<PropertyContact> propertyContactList = propertyContactService.findAllByPhone(request.getParam().getMemberPhone());
        List<String> propertyUuidList = propertyContactList.stream().map(PropertyContact::getPropertyUuid).collect(Collectors.toList());

        if (!CollectionUtils.isEmpty(propertyUuidList)) {
            List<Property> propertyList = propertyService.findAllSaleProperty(request.getParam().getTradeStatus(), propertyUuidList);
            result = propertyList.parallelStream().map(propertyMapper::domainToSaleListDto).collect(Collectors.toList());
            if (CollectionUtils.isEmpty(result)){
                return DataResponse.ok();
            }
            if (StringUtils.isNotBlank(request.getParam().getTradeStatus())){
                Optional.ofNullable(result).orElseGet(Lists::newArrayList)
                        .stream().map(property->{
                    if (StringUtils.isNotBlank(property.getTradeType())){
                        property.setTradeType(request.getParam().getTradeStatus());
                    }
                    return property;
                }).collect(Collectors.toList());
            }
        }
        return DataResponse.ok(result);
    }


    @PostMapping("/searchMyRentProperty")
    @ApiOperation(value = "查询我的租房列表", notes = "查询我的租房列表")
    public DataResponse<List<RentPropertyListDto>> searchMyRentProperty(@RequestBody @Valid DataRequest<PropertyContactQueryDto> request) {
        List<RentPropertyListDto> result = null;

        List<PropertyContact> propertyContactList = propertyContactService.findAllByPhone(request.getParam().getMemberPhone());
        List<String> propertyUuidList = propertyContactList.stream().map(PropertyContact::getPropertyUuid).collect(Collectors.toList());

        if (!CollectionUtils.isEmpty(propertyUuidList)) {

            List<Property> propertyList = propertyService.findAllRentProperty(request.getParam().getTradeStatus(), propertyUuidList);
            result = propertyList.parallelStream().map(propertyMapper::domainToRentListDto).collect(Collectors.toList());
            if (CollectionUtils.isEmpty(result)){
                return DataResponse.ok();
            }
            if (StringUtils.isNotBlank(request.getParam().getTradeStatus())) {
                Optional.ofNullable(result).orElseGet(Lists::newArrayList)
                        .stream().map(property -> {
                    if (StringUtils.isNotBlank(property.getTradeType())) {
                        property.setTradeType(request.getParam().getTradeStatus());
                    }
                    return property;
                }).collect(Collectors.toList());
            }
        }
        return DataResponse.ok(result);
    }

    @PostMapping("/addListedRecording")
    @ApiOperation(value = "添加挂牌数据", notes = "添加挂牌数据")
    public DataResponse<Object> addListedRecording(@RequestBody @Valid DataRequest<MyHouseAddDto> request) {
        MemberHouseInfo memberHouseInfo = new MemberHouseInfo();
        BeanUtils.copyProperties(request.getParam(),memberHouseInfo);
        memberHouseInfo.setCorporationCode(QfContextHolder.getContext().getCorporationCode());
        memberHouseInfo.setCreateTime(new Date());
        return DataResponse.ok(memberHouseInfoService.save(memberHouseInfo));
    }

    @PostMapping("/getListedRecording")
    @ApiOperation(value = "查看挂牌数据", notes = "查看挂牌数据")
    public DataResponse<MyHouseInfoListDto> getListedRecording(@RequestBody @Valid DataRequest<MyHouseInfoQueryDto> request) {
        return DataResponse.ok(memberHouseInfoService.getListedRecording(request.getParam()));
    }

}
